layui.use(function () {
    var tree = layui.tree;
    var table = layui.table;
    var $ = layui.jquery;

    listFile()


    function listFile() {

        // 已知数据渲染
        table.render({
            id: "sharedir_table",
            elem: '#sharedir_table',
            toolbar: '#toolbarDemo',
            url: '/shareDir/list',
            cols: [[ //标题栏
                {field: 'dirName', title: '文件夹名称', minWidth: 200},
                {field: 'ip', title: 'ip', width: 160},
                {field: 'uuid', title: '共享编码', width: 280},
                {field: 'createTime', title: '创建时间', width: 180},
                {field: 'city', title: '操作', width: 220, toolbar: '#barDemo'},
            ]],
            height: '560px',
            skin: 'grid', // 表格风格
            page: false, // 是否显示分页
            parseData: function (res) { // res 即为原始返回的数据

                const loadUrl = window.location.origin;
                const data = res.data;
                data.forEach((item) => {
                    if (loadUrl.indexOf(item.ip) !== -1) {
                        item.LAY_CHECKED = true;
                    }
                });

                return {
                    "code": res.code === 200 ? 0 : res.code, // 解析接口状态
                    "msg": res.msg, // 解析提示文本
                    "data": data// 解析数据列表
                };
            },
            done: function () {

                // 工具栏事件
                table.on('toolbar(sharedir_table)', function (obj) {
                    var id = obj.config.id;
                    var checkStatus = table.checkStatus(id);
                    switch (obj.event) {
                        case 'create':
                            layer.prompt({title: '创建文件夹', formType: 0}, function (pass, index) {
                                layer.close(index);

                                $.post('/shareDir/create', {
                                    dirName: pass
                                }, function (res) {
                                    if (200 === res.code) {
                                        table.reloadData("sharedir_table")
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 500});
                                    }
                                }, 'JSON');

                            });
                            break;
                    }
                });

                table.on('tool(sharedir_table)', function (obj) {
                    const loadUrl = window.location.origin;

                    if (loadUrl.indexOf(obj.data.ip) == -1) {
                        layer.msg("非本计算机文件, 无法查看", {icon: 5, time: 500});
                        return;
                    }

                    if (obj.event === 'open') {
                        window.location.href = ("/main?dir=" + obj.data.dirName);
                    } else if (obj.event === 'share') {

                        const rootUrl = "http://" + obj.data.ip + "/main?shareCode=";
                        const url = rootUrl + obj.data.uuid

                        $("#share_url").html(url)
                        layer.open({
                            title: "共享链接",
                            type: 1,
                            content: $("#layer-wrapper"),
                        });
                    } else if (obj.event === 'delete') {
                        layer.confirm('确定删除文件？', {
                            btn: ['确定', '关闭'] //按钮
                        }, function () {

                            $.post('/shareDir/remove', {
                                id: obj.data.id
                            }, function (res) {
                                if (200 === res.code) {
                                    table.reloadData("sharedir_table")
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5, time: 500});
                                }
                            }, 'JSON');

                        }, function () {

                        });
                    } else if (obj.event === 'package') {

                        var form = $('<form action="/shareDir/package" method="POST">' +
                            '<input type="hidden" name="folderPath" value="' + obj.data.dirName + '">' +
                            '</form>');
                        $('body').append(form);
                        form.submit().remove();

                    }
                });


            }
        });
    }


});